package com.zzptc.tkfate;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.tkfate.entity.Cars;
import com.zzptc.tkfate.entity.Guzhang;
import com.zzptc.tkfate.entity.Youjia;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getCarInfo();
        //getYoujiaInfo();
        getGuzhangInfo();
    }

    private void getGuzhangInfo() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/obdcode/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        GuzhangService guzhangService = retrofit.create(GuzhangService.class);
        Map<String,String> map=new HashMap<>();
        map.put("key","6b40135084049a9641f5dc01af7ed188");
        map.put("code","P2079");
        Call<Guzhang> call= guzhangService.getGuzhangxinxi(map);
        call.enqueue(new Callback<Guzhang>() {
            @Override
            public void onResponse(Call<Guzhang> call, Response<Guzhang> response) {
                Guzhang guzhang = response.body();
                Log.d(TAG,"onResponse:"+guzhang.getResult().getGzfw());
            }

            @Override
            public void onFailure(Call<Guzhang> call, Throwable t) {

            }
        });
    }

    private void getYoujiaInfo() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/gnyj/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        YoujiaService youjiaService = retrofit.create(YoujiaService.class);
        Call<Youjia> call = youjiaService.getAll("e0106d4f036cf6d539ae0469d19545b2");
        call.enqueue(new Callback<Youjia>() {
            @Override
            public void onResponse(Call<Youjia> call, Response<Youjia> response) {
                Youjia youjia = response.body();
                Log.d(TAG,"onResponse:"+youjia.getError_code());
            }

            @Override
            public void onFailure(Call<Youjia> call, Throwable t) {

            }
        });

    }

    private void getCarInfo() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CarsService carsService = retrofit.create(CarsService.class);
        Call<Cars> all=carsService.getAll();
        all.enqueue(new Callback<Cars>() {
            @Override
            public void onResponse(Call<Cars> call, Response<Cars> response) {
                Cars carlist=response.body();
                Log.d(TAG, "onResponse: "+carlist.getData().get(1).getCarno());
            }

            @Override
            public void onFailure(Call<Cars> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });
    }
}